﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Frame.Lib.Core.User;
using Frame.Lib.Core.Role;
using Frame.Lib.Enums;
using Frame.Lib.Utilities;


public partial class Admin_Role_SaveUser : PageSelectTree
{
    UserBO user
    {
        get
        {
            return UserBO.Instance<UserBO>();
        }
    }
    RoleBO role
    {
        get
        {
            return RoleBO.Instance<RoleBO>();
        }
    }

    private int RoleID
    {
        get
        {
            return int.Parse(StrUtil.DecryptUrl(Request["RoleID"]));
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindTree();
            if (RoleID < 0)
            {
                lblName.Text = ((InternalRole)RoleID).ToString();
            }
            else
            {
                lblName.Text = (new RoleBO()).GetName(RoleID);
            }
        }
    }

    private void BindTree()
    {
        List<int> lstUserIDs = role.GetUserIDs(RoleID);
        for (int i = 0; i < lstUserIDs.Count; i++)
        {
            lstUserIDs[i] = -lstUserIDs[i];
        }
        user.BindDeptUserTree(tvDeptUser, true, lstUserIDs);
        
        tvDeptUser.Attributes.Add("onclick", "SelectUser()");
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        string[] strUserID = CheckedIDs.Split(',');

        List<int> lstUserIDs = new List<int>();
        foreach (string str in strUserID)
        {
            if (str != "")
            {
                int id = int.Parse(str);
                if (id < 0)
                {
                    lstUserIDs.Add(-id);
                }
            }
        }
        
       
        role.SaveUserIDs(RoleID, lstUserIDs);
        string strScript = "window.close();";
        JSUtil.ResponseJS(strScript);
    }
}
